﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LiJu
{
    public partial class UserManager : Form
    {
        public UserManager()
        {
            InitializeComponent();
        }

        AutoSizeFormClass asc = new AutoSizeFormClass();
        private void AddUserbutton_Click(object sender, EventArgs e)
        {
            AddUser addUser = new AddUser();
            addUser.ShowDialog();
        }

        private void UserManager_Load(object sender, EventArgs e)
        {
            LoadUserData();

            systemLog1.Visible = false;
            roleManager1.Visible = false;
            userInfoData1.Visible = true;

        }

        public void LoadUserData()
        {
            //DataTable dataTable = BLL.Bll.GetUserManagerData();
            //this.IdColumn.DataPropertyName = "Id";
            //this.UserIdColumn.DataPropertyName = "UserId";
            //this.UserNameColumn.DataPropertyName = "UserName";
            //this.UserRoleColumn.DataPropertyName = "UserRole";
            //this.StateColumn.DataPropertyName = "State";
            //this.OperateColumn.DataPropertyName = "Operate";
            //// Id,UserId,UserName,UserRole,State

            //UserInfodataGridView.DataSource = dataTable;

            //UserInfodataGridView.AllowUserToAddRows = false;

            ////循环遍历添加每行的控件
            //for (int i = 0; i < UserInfodataGridView.Rows.Count; i++)
            //{
            //    if (UserInfodataGridView.Rows[i].Cells[5].Value.ToString() == "禁用")
            //    {
            //        MulAutoBtnEdit(i, "启用");
            //    }
            //    else
            //    {
            //        MulAutoBtnEdit(i, "禁用");
            //    }

            //}
        }


        #region 

        /// <summary>
        /// 绘制单元格内的控件
        /// </summary>
        /// <param name="rowIndex"></param>
        private void MulAutoBtnEdit(int rowIndex, string disable)
        {

            //int index = this.UserInfodataGridView.Columns["OperateColumn"].Index;
            //Label btnAdd = GetBtnByType("BtnAdd", "编辑", rowIndex);
            //Label btnEdit = GetBtnByType("BtnEdit", disable, rowIndex);
            //Label btnDel = GetBtnByType("BtnDel", "删除", rowIndex);
            //this.UserInfodataGridView.Controls.Add(btnAdd);
            //this.UserInfodataGridView.Controls.Add(btnEdit);
            //this.UserInfodataGridView.Controls.Add(btnDel);
            //Rectangle rectangle = this.UserInfodataGridView.GetCellDisplayRectangle(index, rowIndex, true);//获取当前单元格上的矩形区域
            //btnAdd.Size = btnEdit.Size = btnDel.Size = new Size(rectangle.Width / 3 + 1, rectangle.Height);
            //btnAdd.Location = new Point(rectangle.Left + 10, rectangle.Top + 8);
            //btnEdit.Location = new Point(rectangle.Left + btnAdd.Width + 10, rectangle.Top + 8);
            //btnDel.Location = new Point(rectangle.Left + btnAdd.Width + btnDel.Width + 10, rectangle.Top + 8);
        }

        /// <summary>
        /// 封装需要显示的控件
        /// </summary>
        /// <param name="strBtnName"></param>
        /// <param name="strBtnText"></param>
        /// <param name="rowIndex"></param>
        /// <returns></returns>
        private Label GetBtnByType(string strBtnName, string strBtnText, int rowIndex)
        {
            Label lab = new Label();
            lab.Name = strBtnName;
            lab.Text = strBtnText;
            lab.ForeColor = strBtnName == "BtnDels" ? Color.Red : Color.FromArgb(64, 158, 255);
            lab.BackColor = Color.Transparent;
            lab.Cursor = Cursors.Hand;
            lab.Tag = rowIndex.ToString();
            lab.AutoSize = true;
            lab.Click += lab_Click;
            return lab;
        }

        /// <summary>
        /// 控件对应的点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void lab_Click(object sender, EventArgs e)
        {
            if (sender is Label)
            {
                Label btn = (Label)sender;
                int index = Convert.ToInt32(btn.Tag);

                if (btn.Text == "编辑")
                {
                    AddUser addUser = new AddUser();
                    addUser.ShowDialog();
                }
                if (btn.Text == "启用")
                {
                    MessageInfo messageInfo = new MessageInfo();
                    messageInfo.Message = "启用";
                    messageInfo.ShowDialog();
                }
                if (btn.Text == "禁用")
                {
                    MessageInfo messageInfo = new MessageInfo();
                    messageInfo.Message = "禁用";
                    messageInfo.ShowDialog();
                }

                if (btn.Text == "删除")
                {
                    WarningInfo warningInfo = new WarningInfo();
                    warningInfo.ShowDialog();
                }

                //MessageBox.Show(string.Format("{0}的数据", btn.Text));
            }
        }

        #endregion

        private void UserManagerbutton_Click(object sender, EventArgs e)
        {
            systemLog1.Visible = false;
            roleManager1.Visible = false;
            userInfoData1.Visible = true;
        }

        private void RoleManagerbutton_Click(object sender, EventArgs e)
        {
            systemLog1.Visible = false;
            roleManager1.Visible = true;
            userInfoData1.Visible = false;
        }

        private void SystemLogbutton_Click(object sender, EventArgs e)
        {
            systemLog1.Visible = true;
            roleManager1.Visible = false;
            userInfoData1.Visible = false;
        }
    }
}
